﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Administration;

namespace TSB.FarmPowerShellWizard.Repository
{
    public class ServicesRepository
    {
        public string GenerateScript(string serverName)
        {
            var server = SPFarm.Local.Servers.Where(srv => srv.Name == serverName).First();

            StringBuilder ps = new StringBuilder();

            if (server != null)
            {
                ps.AppendLine(Helpers.GetScriptComment("SOS_StartServinceInstance").Replace("{servername}", serverName));


                var provisionCmd = Helpers.GetScriptComment("SOS_Provision");
                foreach(var service in server.ServiceInstances)
                {
                    if (service.Status == SPObjectStatus.Online)
                    {
                        ps.Append(provisionCmd.Replace("{servicename}", service.TypeName));
                    }
                }
            }

            return ps.ToString();
        }
    }
}
